<?php
/**
 * hognyi @taoding 2018.11.19
 *
 *
 */
namespace App\Logic;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Resources\Agent as AgentResource;
use App\Models\Agent;


class AgentLogic
{
	
	/**
	 * 表单数据
	 * @var array
	 */
	private $_post_data = array();
	
		
	public function __construct()
	{
		
	}
	
	// 分页获取代理商列表
	static public function getAgentList($where, $page = 1, $limit = 20)
	{
		
		return AgentResource::collection(Agent::where($where)->orderBy('sort', 'desc')->paginate($limit))->additional(['code' =>200,'status' => 'success']);
	
	}
	
	//获取客户详情
	static public function getCompanyDetail($where){
		
		
	}
	
	//创建代理商	
	public function create($request){	

		$this->_post_data = $request;
		
		try {
			DB::beginTransaction();
			//第1步 执行下单
			$order_info = $this->_create();
			return $order_info;
		} catch (\Exception $e) {
			DB::rollBack();
			return ['error' => $e->getMessage()];
		}						
	}
	
	public function _create(){
		
		dd($this->_post_data['company_name']);
		//代理商
		$agent_insert['company_name'] = $this->_post_data['company_name'];
		$agent_insert['address'] = $this->_post_data['address'];
		$agent_insert['company_type'] = $this->_post_data['company_type'];
		$agent_insert['balance'] = $this->_post_data['balance'];
		$agent_insert['ratio'] = $this->_post_data['ratio'];
		$agent_insert['bank_name'] = $this->_post_data['bank_name'];
		$agent_insert['bank_account'] = $this->_post_data['bank_account'];
		dd($agent_insert);
		//公司联系人
		$linkman_insert['legal_person'] = $this->_post_data['legal_person'];
		$linkman_insert['legal_person_phone'] = $this->_post_data['legal_person_phone'];
		$linkman_insert['linkman_name'] = $this->_post_data['linkman_name'];
		$linkmant_insert['linkman_phone'] = $this->_post_data['linkman_phone'];
		
		//木牛盒子登录账号
		$user_insert['login_name'] = $this->_post_data['login_name'];
		$user_insert['password'] = $this->_post_data['password'];

	
		dd($this->_post_data);
		
		// 执行插入
		$agent_model = Agent::create($agent_insert);
		
		$link_man_model = DB::table('link_man')->insert($linkman_insert);
		$sys_user_model = DB::table()->insert($user_insert);
		
		if ($sys_user_model) {
			DB::commit();
			return $agent_model->toArray();
		} else {
			$this->pay_log('$agent_model.error' . var_export($agent_model, true));
			throw new \Exception('代理商保存保存失败');
		}
	}
	
}
